package com.power.using.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.power.using.myapplication.R;
import com.power.using.myapplication.base.BaseMainFragment;
import com.power.using.myapplication.eventbus.EventCenter;
import com.power.using.myapplication.interfaces.Constant;
import com.power.using.myapplication.interfaces.DialogClick;
import com.power.using.myapplication.utils.GlideCacheUtil;
import com.power.using.myapplication.utils.LogUtils;
import com.power.using.myapplication.utils.SPUtil;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2018/5/25.
 * Time: 2018/5/25  10:35
 */

public class FourFrament extends BaseMainFragment {

    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_iv_menu)
    ImageView toolbarIvMenu;
    @BindView(R.id.toolbar_tv_menu)
    TextView toolbarTvMenu;
    @BindView(R.id.toolbar_rl_menu)
    AutoRelativeLayout toolbarRlMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.comple_name)
    TextView compleName;
    @BindView(R.id.compele_type)
    TextView compeleType;
    @BindView(R.id.operator)
    TextView operator;
    @BindView(R.id.chang_mi_img)
    ImageView changMiImg;
    @BindView(R.id.change_mi_rl)
    AutoRelativeLayout changeMiRl;
    @BindView(R.id.sys_setting_img)
    ImageView sysSettingImg;
    @BindView(R.id.setting_rl)
    AutoRelativeLayout settingRl;
    @BindView(R.id.clear_cache_img)
    ImageView clearCacheImg;
    @BindView(R.id.image_cache)
    TextView imageCache;
    @BindView(R.id.wipe_cache)
    AutoRelativeLayout wipeCache;
    @BindView(R.id.about_img)
    ImageView aboutImg;
    @BindView(R.id.about_rl)
    AutoRelativeLayout aboutRl;
    @BindView(R.id.personal_btn_quit)
    Button personalBtnQuit;
    Unbinder unbinder;
  //  private Banner homeMZBanner;
    private RecyclerView homeRV;
    private TextView toolTitle;
    private ImageView mMenu;

    private static final int[] image = {R.drawable.banner_four, R.drawable.banner_two, R.drawable.banner_three};
    private final String[] attr_name = {"公安部门", "生产企业", "经营企业", "购买使用企业", "运输企业"};


    public static FourFrament newInstance() {

        Bundle args = new Bundle();

        FourFrament fragment = new FourFrament();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_four, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        return view;
    }

    private void initView(View view) {
        EventBus.getDefault().register(this);
        //隐藏标题栏
        toolbarRlMenu.setVisibility(View.GONE);
        toolbarTitle.setText("个人中心");

    }


    /**
     * //懒加载--->再懒加载中加载数据
     * @param savedInstanceState
     */
    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initData();
    }


    /**
     * 加载数据
     */
    private void initData() {

        String opt_name = null;
        String mCompeletype = null;
//        UserInforEntity inforEntity = Util.getUserInforEntity();
//        if (inforEntity == null) return;
//        opt_name = inforEntity.getOptname();
//        mCompeletype =inforEntity.getTypeid();
//
//        compleName.setText(inforEntity.getOrgname());
//        // 操作员
//        operator.setText(opt_name);
//        // 企业类型
//        getAttrName(Integer.valueOf(mCompeletype));

    }


    private void getAttrName(Integer mCompeletype) {
        switch (mCompeletype) {
            case 131:
                compeleType.setText(attr_name[0]);
                break;
            case 132:
                compeleType.setText(attr_name[1]);
                break;
            case 133:
                compeleType.setText(attr_name[2]);
                break;
            case 134:
                compeleType.setText(attr_name[3]);
                break;
            case 135:
                compeleType.setText(attr_name[4]);
                break;

        }
    }

    /**
     * 这个方法什么也没做
     * 只是这个页面注册了EventBus,相应的也应该有EventBus接受事件
     * @param eventcenter
     */
    @Subscribe
    public void onEvenThread(EventCenter eventcenter) {

    }


    /**
     * 对应的点击事件
     * @param view
     */
    @OnClick({R.id.change_mi_rl, R.id.setting_rl, R.id.wipe_cache, R.id.about_rl, R.id.personal_btn_quit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.change_mi_rl:// 修改密码
                //EventBus.getDefault().post(new StartNewFragmentEvent(ChangePassWordFragment.newInstance()));
                break;
            case R.id.setting_rl:// 系统设置

                break;
            case R.id.wipe_cache:// 清理缓存
                showClearDialog();
                break;
            case R.id.about_rl:// 关于我们
               // EventBus.getDefault().post(new StartNewFragmentEvent(AboutFragment.newInstance()));

                break;
            case R.id.personal_btn_quit:
                showQuitDialog();
                break;
        }
    }


    /**
     * 退出应用
     */
    private void showQuitDialog() {
        LogUtils.e("显示", "推出");

        SPUtil.remove(Constant.KEY_USERNAME);
        SPUtil.remove(Constant.KEY_USERPWD);
        SPUtil.remove(Constant.KEY_IMEI);
//        PersistentCookieStore store = new PersistentCookieStore(_mActivity.getApplicationContext());
//        store.removeAll();
//        Intent intent = new Intent();
//        intent.setClass(getActivity(), LoginActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivity(intent);
        _mActivity.finish();

    }

    /**
     * 清除缓存
     */
    private void showClearDialog() {
        showAlertDialogNoMsg(getActivity(), "确定清除图片缓存？", "", new DialogClick() {
            @Override
            public void PositiveMessage() {
                GlideCacheUtil.getInstance().clearImageAllCache(getActivity());
                showShortToast(getActivity(), "清除成功");
                imageCache.setText("0.0");
            }

            @Override
            public void NegativeMessage() {

            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }
}
